﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;

using AvailableRooms.Exchange;
using System.Net.Security;

namespace AvailableRooms
{
    public class ExchangeService
    {
        private ExchangeServiceBinding _ExchangeService;

        #region Properties

        public ICredentials Credentials
        {
            get { return _ExchangeService.Credentials; }
            set { _ExchangeService.Credentials = value; }
        }

        public String Url
        {
            get { return _ExchangeService.Url; }
            set { _ExchangeService.Url = value; }
        }

        public RequestServerVersion RequestServerVersionValue
        {
            get { return _ExchangeService.RequestServerVersionValue; }
            set { _ExchangeService.RequestServerVersionValue = value; }
        }
        
        #endregion

        public ExchangeService()
        {
            ServicePointManager.ServerCertificateValidationCallback =
            delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            };

            _ExchangeService = new ExchangeServiceBinding();
        }

        public List<TimeZoneDefinitionType> GetServerTimeZones()
        {
            GetServerTimeZonesResponseMessageType response = (GetServerTimeZonesResponseMessageType)_ExchangeService.GetServerTimeZones(new GetServerTimeZonesType() { ReturnFullTimeZoneData = true }).ResponseMessages.Items[0];

            if(response.TimeZoneDefinitions.TimeZoneDefinition != null)
                return response.TimeZoneDefinitions.TimeZoneDefinition.ToList<TimeZoneDefinitionType>();

            return new List<TimeZoneDefinitionType>();
        }

        public List<EmailAddressType> GetRoomLists()
        {
            GetRoomListsResponseMessageType response = _ExchangeService.GetRoomLists(new GetRoomListsType());
            
            if(response.RoomLists != null)
                return response.RoomLists.ToList<EmailAddressType>();

            return new List<EmailAddressType>();
        }

        public List<RoomType> GetRooms(EmailAddressType emailAddressType)
        {
            GetRoomsResponseMessageType response = _ExchangeService.GetRooms(new GetRoomsType() { RoomList = emailAddressType });

            if(response.Rooms != null)
                return response.Rooms.ToList<RoomType>();

            return new List<RoomType>();
        }

        public List<CalendarEvent> GetRoomAvailability(List<MailboxData> MailBoxes, SerializableTimeZone TimeZone, FreeBusyViewOptionsType fbViewOptions)
        {
            // Make the request.
            GetUserAvailabilityRequestType request = new GetUserAvailabilityRequestType();

            // Set the time zone of the request.
            request.TimeZone = TimeZone;

            // Add the mailboxes to the request.
            request.MailboxDataArray = MailBoxes.ToArray();

            // Add the free/busy view options to the request.
            request.FreeBusyViewOptions = fbViewOptions;

            GetUserAvailabilityResponseType response = _ExchangeService.GetUserAvailability(request);

            if(response.FreeBusyResponseArray.FirstOrDefault().FreeBusyView.CalendarEventArray != null)
                return response.FreeBusyResponseArray.FirstOrDefault().FreeBusyView.CalendarEventArray.ToList<CalendarEvent>();

            return new List<CalendarEvent>();
        }
    }
}
